package com.luolc.syllabustest.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luolc.syllabustest.R;

import java.util.ArrayList;

/**
 * Name:自定义课程时间控件
 * Author: WK（wangkun@xjzhicheng.com)
 * Date: 2016-10-13
 * Describe: PLEASE TO FIXME...
 */
public class SyllabusClassTimeInfoView extends LinearLayout {


    private ImageButton iBtnDel;
    private TextView tvTitle;//标题
    private TextView tvWeek;//周
    private TextView tvClassTime;//具体课程时间
    private EditText tvClassLocation;//地址
    private WeekChoiceDialog weekChoiceDialog;//周选择器
    private ClassTimeChoiceDialog classTimeChoiceDialog;//课程时间选择器

    private int mPosition;

    private IRemoveClassTimeItemListener iRemoveClassTimeItemListener;


    //删除自己接口
    public interface IRemoveClassTimeItemListener {
        void onClick(View view);
    }

    //时间冲突验证接口
    public interface ITimeCheckListener {
        void onCheck();
    }

    //Week and ClassTime冲突验证接口
    public interface ITimeConflictCheckListener {
        void onCheck(View view, ArrayList<String> weekData, ArrayList<Integer> classTimeData);
    }


    public SyllabusClassTimeInfoView(Context context) {
        this(context, null);
    }

    public SyllabusClassTimeInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.syllabus_add_class_time_item, this);
        initView();
    }

    /**
     * 初始化View
     */
    private void initView() {
        weekChoiceDialog = getWeekChoiceDialog();
        classTimeChoiceDialog = getTimeChoiceDialog();
        bindView();
        setUpListener();
    }

    /**
     * 绑定View视图
     */
    private void bindView() {
        tvTitle = (TextView) findViewById(R.id.tv_class_time_title);
        iBtnDel = (ImageButton) findViewById(R.id.ibtn_del_item);
        tvWeek = (TextView) findViewById(R.id.tv_week);
        tvClassTime = (TextView) findViewById(R.id.tv_class_time);
        tvClassLocation = (EditText) findViewById(R.id.tv_class_location);
    }

    /**
     * 初始化监听
     */
    private void setUpListener() {
        tvTitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                removeMyself(SyllabusClassTimeInfoView.this);
            }
        });
        iBtnDel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                removeMyself(SyllabusClassTimeInfoView.this);
            }
        });

        tvWeek.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹出单，双周，混周选择
                //设置数据（bug）
                weekChoiceDialog.setDataSource((ArrayList<String>) v.getTag());
                weekChoiceDialog.show();
            }
        });
        tvClassTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置数据（bug）
                classTimeChoiceDialog.setDataSource(((ArrayList<Integer>) v.getTag()));
                classTimeChoiceDialog.show();
            }
        });
    }

    /**
     * 移除自己
     *
     * @param view this视图
     */
    private void removeMyself(View view) {
        iRemoveClassTimeItemListener.onClick(view);
    }

    /**
     * 获得Week选择器
     *
     * @return WeekChoiceDialog
     */
    private WeekChoiceDialog getWeekChoiceDialog() {
        WeekChoiceDialog weekChoiceDialog = new WeekChoiceDialog(getContext());
        weekChoiceDialog.setIGetChoiceDataSourceListener(new WeekChoiceDialog.IGetChoiceDataSource() {
            @Override
            public void onGetData(ArrayList<String> data) {
                int size = data.size();
                if (size == 0) {
                    tvWeek.setText("");
                } else if (size == 1) {
                    tvWeek.setText(data.get(0));
                } else {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (String s : data) {
                        stringBuilder.append(s);
                        stringBuilder.append(",");
                    }
                    stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
                    tvWeek.setText(stringBuilder.toString());
                }
                tvWeek.setTag(data.clone());
            }
        });
        return weekChoiceDialog;
    }

    /**
     * 获得ClassTime选择器
     *
     * @return ClassTimeChoiceDialog
     */
    private ClassTimeChoiceDialog getTimeChoiceDialog() {
        ClassTimeChoiceDialog classTimeChoiceDialog = new ClassTimeChoiceDialog(getContext());
        classTimeChoiceDialog.setIGetChoiceDataSourceListener(new ClassTimeChoiceDialog.IGetChoiceDataSource() {
            @Override
            public void onGetData(ArrayList<Integer> data) {
                StringBuilder stringBuilder = new StringBuilder();
                int size = data.size();
                for (int i = 0; i < size; i++) {
                    if (i == 0) {
                        stringBuilder.append(ClassTimeChoiceDialog.WEEKDAY_TITLES[data.get(0)]);
                        stringBuilder.append(" ");
                    } else {
                        stringBuilder.append(data.get(i));
                        if (i == size - 1) {
                            stringBuilder.append("节");
                        } else {
                            stringBuilder.append("-");
                        }
                    }
                }
                tvClassTime.setText(stringBuilder.toString());
                tvClassTime.setTag(data.clone());
            }
        });
        return classTimeChoiceDialog;
    }

    //----------------------------------------Getter,Setter-----------------------------------------


    public void setRemoveClassTimeItemListener(IRemoveClassTimeItemListener iRemoveClassTimeItemListener) {
        this.iRemoveClassTimeItemListener = iRemoveClassTimeItemListener;
    }

    public int getPosition() {
        return mPosition;
    }

    public void setPosition(int mPosition) {
        this.mPosition = mPosition;
        tvTitle.setText("上课时间" + mPosition);
    }

    public void setClassTimeConflictCheck(final ITimeConflictCheckListener classTimeConflictCheck) {
        weekChoiceDialog.setTimeConflictCheckListener(new ITimeCheckListener() {
            @Override
            public void onCheck() {
                classTimeConflictCheck.onCheck(SyllabusClassTimeInfoView.this, getWeekData(), getClassTimeData());
            }
        });
        classTimeChoiceDialog.setTimeConflictCheckListener(new ITimeCheckListener() {
            @Override
            public void onCheck() {
                classTimeConflictCheck.onCheck(SyllabusClassTimeInfoView.this, getWeekData(), getClassTimeData());
            }
        });
    }

    public String getTitle() {
        return tvTitle.getText().toString();
    }

    public String getLocation() {
        return tvClassLocation.getText().toString();
    }

    public ArrayList<String> getWeekData() {
        return weekChoiceDialog.getChoiceDataSource();
    }

    public ArrayList<Integer> getClassTimeData() {
        return classTimeChoiceDialog.getChoiceDataSource();
    }


}
